package com.wei.tang.fragment;

import com.wei.tang.AppConest;
import com.wei.tang.AppContext;
import com.wei.tang.R;
import com.wei.tang.base.BaseFragment;
import com.wei.tang.bean.UserBean;
import com.wei.tang.ui.course.StudentWorkActivity;
import com.wei.tang.ui.course.TeacherWorkActivity;
import com.wei.tang.ui.me.AddStudentActivity;
import com.wei.tang.ui.me.ApplyTeacherActivity;
import com.wei.tang.ui.me.MyInformationActivity;
import com.wei.tang.ui.me.MyStudentActivity;
import com.wei.tang.ui.me.RegistLoginActivity;
import com.wei.tang.ui.me.SettingActivity;
import com.wei.tang.util.Tlog;
import com.wei.tang.widget.EmptyLayout;
import com.wei.tang.widget.RoundImageView;
import com.wei.tang.widget.dialog.DialogSmallCancel;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MyInformationFragment extends BaseFragment {
	private Activity mySelf = null;

	@InjectView(R.id.rl_app_head_left)
	RelativeLayout rl_app_head_left;
	@InjectView(R.id.tv_app_head_left_content)
	TextView tv_app_head_left_content;
	@InjectView(R.id.iv_app_head_left_image)
	ImageView iv_app_head_left_image;
	@InjectView(R.id.rl_app_head_center)
	RelativeLayout rl_app_head_center;
	@InjectView(R.id.tv_app_head_center_content)
	TextView tv_app_head_center_content;
	@InjectView(R.id.iv_app_head_center_image)
	ImageView iv_app_head_center_image;
	@InjectView(R.id.rl_app_head_right)
	RelativeLayout rl_app_head_right;
	@InjectView(R.id.tv_app_head_right_content)
	TextView tv_app_head_right_content;
	@InjectView(R.id.iv_app_head_right_image)
	ImageView iv_app_head_right_image;

	@InjectView(R.id.ll_my_info_head)
	LinearLayout ll_my_info_head;
	@InjectView(R.id.ll_my_info_study)
	LinearLayout ll_my_info_study;
	@InjectView(R.id.ll_my_info_apply)
	LinearLayout ll_my_info_apply;

	@InjectView(R.id.ll_my_info_data)
	LinearLayout ll_my_info_data;
	@InjectView(R.id.tv_my_info_data)
	TextView tv_my_info_data;

	@InjectView(R.id.tv_my_info_study)
	TextView tv_my_info_study;
	@InjectView(R.id.tv_my_info_apply)
	TextView tv_my_info_apply;

	@InjectView(R.id.iv_my_info_head)
	RoundImageView iv_my_info_head;

	@InjectView(R.id.iv_my_info_name)
	TextView iv_my_info_name;
	@InjectView(R.id.iv_my_info_mark)
	TextView iv_my_info_mark;
	@InjectView(R.id.iv_my_info_decs)
	TextView iv_my_info_decs;

	@InjectView(R.id.error_layout_head)
	EmptyLayout error_layout_head;

	// 用户信息
	private UserBean userBean = null;

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
			@Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.my_information_fragment, container, false);
		ButterKnife.inject(this, view);
		initData();
		initView(view);
		return view;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mySelf = activity;
	}

	@Override
	public void initView(View view) {
		tv_app_head_center_content.setText("我");
		updateShow();
	}

	void updateShow() {
		if (AppConest.ISSTUDENT) {
			tv_my_info_study.setText("学习任务");
			tv_my_info_apply.setText("申请成为老师");
			iv_my_info_mark.setText("同学");
			tv_my_info_data.setText("我的老师");
		} else {
			tv_my_info_study.setText("教学任务");
			tv_my_info_apply.setText("添加学生");
			iv_my_info_mark.setText("老师");
			tv_my_info_data.setText("我的学生");
		}
		if (AppConest.ISLOGIN) {
			iv_my_info_name.setText(userBean.getUser_name());
			iv_my_info_decs.setText(userBean.getDecription());
		}
	}

	@Override
	public void initData() {
		if (!AppConest.ISLOGIN) {
			error_layout_head.setErrorType(EmptyLayout.NOLOGIN);
		} else {
			error_layout_head.setErrorType(EmptyLayout.HIDE);
			userBean = AppContext.USERBEAN;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (!AppConest.ISLOGIN) {
			error_layout_head.setErrorType(EmptyLayout.NOLOGIN);
		} else {
			userBean = AppContext.USERBEAN;

			if (userBean.getUser_status().equals("s")) {
				AppConest.ISSTUDENT = true;
			} else {
				AppConest.ISSTUDENT = false;
			}

			error_layout_head.setErrorType(EmptyLayout.HIDE);
		}
		updateShow();
	}

	@Override
	@OnClick({ R.id.ll_my_info_head, R.id.ll_my_info_study, R.id.ll_my_info_apply, R.id.btn_setting,
			R.id.error_layout_head, R.id.ll_my_info_data })
	public void onClick(View v) {
		Intent intent = null;
		Bundle bundle = null;
		int id = v.getId();
		switch (id) {
		case R.id.ll_my_info_head:
			intent = new Intent(mySelf, MyInformationActivity.class);
			startActivity(intent);
			break;
		case R.id.ll_my_info_data:
			intent = new Intent(mySelf, MyStudentActivity.class);
			startActivity(intent);
			break;
		case R.id.ll_my_info_study:
			if (AppConest.ISSTUDENT) {
				intent = new Intent(mySelf, StudentWorkActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(mySelf, TeacherWorkActivity.class);
				startActivity(intent);
			}

			break;
		case R.id.btn_setting:
			intent = new Intent(mySelf, SettingActivity.class);
			startActivity(intent);
			break;
		case R.id.ll_my_info_apply:
			if (AppConest.ISSTUDENT) {
				intent = new Intent(mySelf, ApplyTeacherActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(mySelf, AddStudentActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.error_layout_head:
			if (!AppConest.ISLOGIN) {
				intent = new Intent(activity, RegistLoginActivity.class);
				activity.startActivity(intent);
			}
			break;
		}
	}
}
